1 =============================================================================
2 CSVSToolBars Module: CSVSToolBars Project Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 VSPackages are software modules that make up and extend the Visual Studio
9 integrated development environment (IDE) by providing UI elements, services,
10 projects, editors, and designers. VSPackages are the principal architectural
11 unit of Visual Studio, and are the unit of deployment, licensing, and security
12 also. Visual Studio itself is written mostly as a collection of VSPackages.
13 This sample demonstrate how to use the the Visual Studio Integration Package
14 Wizard to create a simple VSPackage with a toolbar.
17 //////////////////////////////////////////////////////////////////////////////
20 VS 2008 SDK must be installed on the machine. You can download it from:
21 http://www.microsoft.com/downloads/details.aspx?FamilyID=30402623-93ca-479a-867c-04dc45164f5b&displaylang=en
23 Otherwise the project may not be opened by Visual Studio.
25 If you run this project on a x64 OS, please also config the Debug tab of the project
26 Setting. Set the "Start external program" to
27 C:\Program Files(x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe
29 NOTE: The Package Load Failure Dialog occurs because there is no PLK(Package Load Key)
30 Specified in this package. To obtain a PLK, please to go to WebSite:
31 http://msdn.microsoft.com/en-us/vsx/cc655795.aspx
33 http://msdn.microsoft.com/en-us/library/bb165395.aspx
35 /////////////////////////////////////////////////////////////////////////////
38 A: How to Create a VSPackage
40 1. Create a new project using Visual Studio Integration Package as template
41 (New Project dialog box -> Other Project Types -> Extensibility).
43 2. In the Location box, type the file path for your VSPackage.
45 3. In the Name box, type the name for the solution and then click OK to start
48 3. On the Select a Programming Language page, select Visual C# and have the
49 wizard generate a key.snk file to sign the assembly, then click Next.
51 4. In the Basic VSPackage Information page, specify details about your
52 VSPackage(Brand the VSPackage) and click Next.
54 5. Select the Menu Command option to create a new command for the VSPackage. Then
57 6. Type the name(All-In-One) and ID(cmdidMyCommand) for the new
58 menu command in the text boxes, then click Next. The command ID is the name
59 of a constant that represents this menu command in the generated code.
61 7. Uncheck the Intergration Test Project and Unit Test Project, then click
64 B: Add a Toolbar to VS IDE
66 1. In Solution Explorer, open CSVSToolbars.vsct.
67 VSCT file stands for Visual Studio Command Table. This is an XML based file that
68 describes the layout and appearance of command items for a VSPackage. Command
69 items include buttons, combo boxes, menus, toolbars, and groups of command items.
71 2. In the <Symbols> section, find the <GuidSymbol> node whose name attribute is
72 guidCSVSToolbarsCmdSet. Add the following two <IDSymbol> elements to the list of
73 <IDSymbol> elements in this node to define a toolbar and a toolbar group.
75 <IDSymbol name="ToolbarID" value="0x1000" />
76 <IDSymbol name="ToolbarGroupID" value="0x1001" />
78 3. Just above the <Groups> section, create an empty <Menus> section, and create the
79 following <Menu> element to define the toolbar that you declared in step 2.
82 <Menu guid="guidCSVSToolbarsCmdSet" id="ToolbarID"
83 priority="0x0000" type="Toolbar">
84 <Parent guid="guidCSVSToolbarsCmdSet" id="ToolbarID" />
86 <ButtonText>My Toolbar</ButtonText>
87 <CommandName>My Toolbar</CommandName>
92 4. Add a new <Group> element to the <Groups> section to define the group that you
93 declared in the <Symbols> section.
95 <Group guid="guidCSVSToolbarsCmdSet" id="ToolbarGroupID"
97 <Parent guid="guidCSVSToolbarsCmdSet" id="ToolbarID"/>
100 5. In the Buttons Element, change the parent of the existing Button Elementto the
101 toolbar group so that the toolbar will be displayed.
103 <Parent guid="guidCSVSToolbarsCmdSet" id="ToolbarGroupID" />
105 6. Save CSVSToolbars.vsct.
108 C: Add a icon to the command:
110 1. Create a bitmap with a color depth of 32-bits. An icon is always 16 x 16 so this
111 bitmap must be 16 pixels high and a multiple of 16 pixels wide.In this sample, it
112 is camera.png, which is a 32bits png image.
114 2. Add the bitmap to the resource of this project
116 3. Open the .vsct file in the editor.
118 4. In the Symbols Element, find the GuidSymbol Element that contains your existing
119 bitmap entries. By default, it is named guidImages. And add new Symbols Element next
122 <GuidSymbol name="guidMyImages" value="{4056cc26-4a2f-432c-a816-e6694e673abb}">
123 <IDSymbol name="bmpMyPic" value="1" />
126 5. Add an IDSymbol Element for each icon in your bitmap. The name attribute is the
127 icon's ID, and the value indicates its position on the strip.
129 <IDSymbol name="bmpMyPic" value="1" />
131 6. Create a Bitmap Element in the <Bitmaps> section of the .vsct file to represent the
132 bitmap containing the icons.
134 <Bitmap guid="guidMyImages" href="Resources\camera.png"/>
136 7.Change the Toolbar button to use the new icon
138 <Icon guid="guidMyImages" id="bmpMyPic" />
140 8. Save all the files.
143 D: Add a menu controller
145 1. Open the .vsct file in the editor.
147 2. In the Symbols Element, in the GuidSymbol Element named "guidCSVSToolbarsCmdSet",
148 declare your menu controller, menu controller group, and three menu items
150 <IDSymbol name="TestMenuController" value="0x1300"/>
151 <IDSymbol name="TestMenuControllerGroup" value="0x1060"/>
152 <IDSymbol name="cmdidMCItem1" value="0x0130"/>
153 <IDSymbol name="cmdidMCItem2" value="0x0131"/>
154 <IDSymbol name="cmdidMCItem3" value="0x0132"/>
156 3.In Menus Element,after the last menu entry, define the menu controller as a menu.
158 <Menu guid="guidCSVSToolbarsCmdSet" id="TestMenuController"
159 priority="0x0100" type="MenuController">
160 <Parent guid="guidCSVSToolbarsCmdSet" id="ToolbarGroupID"/>
161 <CommandFlag>IconAndText</CommandFlag>
162 <CommandFlag>TextChanges</CommandFlag>
163 <CommandFlag>TextIsAnchorCommand</CommandFlag>
165 <ButtonText>Test Menu Controller</ButtonText>
166 <CommandName>Test Menu Controller</CommandName>
170 4. In the Groups Element, after the last group entry, add the menu controller group
172 <Group guid="guidCSVSToolbarsCmdSet" id="TestMenuControllerGroup"
174 <Parent guid="guidCSVSToolbarsCmdSet" id="TestMenuController"/>
177 5. In the Buttons Element, after the last button entry, add a Button Element for each of
180 <Button guid="guidCSVSToolbarsCmdSet" id="cmdidMCItem1"
181 priority="0x0000" type="Button">
182 <Parent guid="guidCSVSToolbarsCmdSet" id="TestMenuControllerGroup"/>
183 <Icon guid="guidImages" id="bmpPic1"/>
184 <CommandFlag>IconAndText</CommandFlag>
186 <ButtonText>MC Item 1</ButtonText>
187 <CommandName>MC Item 1</CommandName>
190 <Button guid="guidCSVSToolbarsCmdSet" id="cmdidMCItem2"
191 priority="0x0100" type="Button">
192 <Parent guid="guidCSVSToolbarsCmdSet" id="TestMenuControllerGroup"/>
193 <Icon guid="guidImages" id="bmpPic2"/>
194 <CommandFlag>IconAndText</CommandFlag>
196 <ButtonText>MC Item 2</ButtonText>
197 <CommandName>MC Item 2</CommandName>
200 <Button guid="guidCSVSToolbarsCmdSet" id="cmdidMCItem3"
201 priority="0x0200" type="Button">
202 <Parent guid="guidCSVSToolbarsCmdSet" id="TestMenuControllerGroup"/>
203 <Icon guid="guidImages" id="bmpPicSearch"/>
204 <CommandFlag>IconAndText</CommandFlag>
206 <ButtonText>MC Item 3</ButtonText>
207 <CommandName>MC Item 3</CommandName>
211 6. Save the .vsct file.
213 E: Implement the menu controller
215 1. Open PkgCmdID.cs and add the following lines
217 public const int cmdidMCItem1 = 0x130;
218 public const int cmdidMCItem2 = 0x131;
219 public const int cmdidMCItem3 = 0x132;
221 2. At the top of the CSVSToolbarsPackage class, add the following:
223 private int currentMCCommand; // The currently selected menu controller command
225 3. In the Initialize method, immediately after the last call to the AddCommand method,
226 add code to route the events for each command through the same handlers.
228 for (int i = PkgCmdIDList.cmdidMCItem1; i <=
229 PkgCmdIDList.cmdidMCItem3; i++)
231 CommandID cmdID = new
232 CommandID(GuidList.guidCSVSToolbarsCmdSet, i);
233 OleMenuCommand mc = new OleMenuCommand(new
234 EventHandler(OnMCItemClicked), cmdID);
235 mc.BeforeQueryStatus += new EventHandler(OnMCItemQueryStatus);
237 // The first item is, by default, checked.
238 if (PkgCmdIDList.cmdidMCItem1 == i)
241 this.currentMCCommand = i;
245 4. Add the following two event handler defined above
247 private void OnMCItemClicked(object sender, EventArgs e)
249 OleMenuCommand mc = sender as OleMenuCommand;
253 switch (mc.CommandID.ID)
255 case PkgCmdIDList.cmdidMCItem1:
256 selection = "Menu controller Item 1";
259 case PkgCmdIDList.cmdidMCItem2:
260 selection = "Menu controller Item 2";
263 case PkgCmdIDList.cmdidMCItem3:
264 selection = "Menu controller Item 3";
268 selection = "Unknown command";
271 this.currentMCCommand = mc.CommandID.ID;
274 (IVsUIShell)GetService(typeof(SVsUIShell));
275 Guid clsid = Guid.Empty;
277 uiShell.ShowMessageBox(
280 "Test Tool Window Toolbar Package",
281 string.Format(CultureInfo.CurrentCulture,
282 "You selected {0}", selection),
285 OLEMSGBUTTON.OLEMSGBUTTON_OK,
286 OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
287 OLEMSGICON.OLEMSGICON_INFO,
293 5. Save the all files.
295 E: Testing the Tool Window
297 1. Press F5 to open a new instance of the Visual Studio experimental build.
299 2. On the View menu, point to Toolbars and then click My Toolbar.
301 3. Click the Load File button on the Tool Window and Select a media file.
304 /////////////////////////////////////////////////////////////////////////////
307 How to: Create VSPackages (C# and Visual Basic)
308 http://msdn.microsoft.com/en-us/library/bb164725.aspx
310 How to: Register a VSPackage (C#)
311 http://msdn.microsoft.com/en-us/library/bb166544.aspx
313 VSPackage Tutorial 1: How to Create a VSPackage
314 http://msdn.microsoft.com/en-us/library/cc138589.aspx
316 Designing XML Command Table (.Vsct) Files
317 http://msdn.microsoft.com/en-us/library/bb166366.aspx
320 http://msdn.microsoft.com/en-us/library/bb165399.aspx
323 http://msdn.microsoft.com/en-us/library/bb165085.aspx
325 Walkthrough: Adding a Toolbar to the IDE
326 http://msdn.microsoft.com/en-us/library/bb164715.aspx
328 How to: Add Icons to Commands on Toolbars
329 http://msdn.microsoft.com/en-us/library/bb165158.aspx
331 How to: Add Menu Controllers to Toolbars
332 http://msdn.microsoft.com/en-us/library/bb166443.aspx
334 Walkthrough: Adding a Menu Controller to a Toolbar
335 http://msdn.microsoft.com/en-us/library/bb165748.aspx
337 Creating a package with a simple command
338 http://dotneteers.net/blogs/divedeeper/archive/2008/01/06/LearnVSXNowPart3.aspx
340 Menus and comands in VS IDE
341 http://dotneteers.net/blogs/divedeeper/archive/2008/02/22/LearnVSXNowPart13.aspx
344 /////////////////////////////////////////////////////////////////////////////